package com.winit.method.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.bean.model.Bean;
import com.winit.bean.service.BeanService;
import com.winit.commons.util.SeqGenerate;
import com.winit.config.model.Config;
import com.winit.config.service.IConfigService;
import com.winit.method.mapper.MethodMapper;
import com.winit.method.model.Method;
import com.winit.method.service.MethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wanglin on 2018/6/4.
 */
@Service
public class MethodServiceImpl implements MethodService {
    @Autowired
    MethodMapper methodMapper;
    @Autowired
    BeanService beanService;
    @Autowired
    private IConfigService configService;

    @Override
    public void batchSgSaveMethod(JSONArray jsonObjectList,String serviceId) {
        if(jsonObjectList==null){
            return;
        }
        for(int i=0;i<jsonObjectList.size();i++){
            JSONObject methodJson = (JSONObject) jsonObjectList.get(i);
            JSONArray mapList=new JSONArray();
            String uri="";
            if(null!=methodJson.getJSONArray("param")){
                for(int p=0;p<methodJson.getJSONArray("param").size();p++){
                    Map map=new HashMap();
                    JSONObject paramJson = (JSONObject) methodJson.getJSONArray("param").get(p);
                    String type = paramJson.getString("type");
                    uri+=type+",";
                    String path = paramJson.getString("path");
                    Bean bean = beanService.findByPathAndName(path, type);
                    if(bean==null){
                        continue;
                    }
                    map.put("beanId",bean.getId());
                    map.put("paramName",bean.getName());
                    map.put("desc",bean.getbDesc());
                    mapList.add(map);
                }
            }
            Method method=new Method();
            Config rtnConfig=new Config();
            if(null != methodJson.getJSONObject("rtn")){
                JSONObject rtnJson = methodJson.getJSONObject("rtn");
                String rtnType = rtnJson.getString("contain") ==null ?rtnJson.getString("type") : rtnJson.getString("contain");
                String rtnPath = rtnJson.getString("path");
                Bean rtnBean = beanService.findByPathAndName(rtnPath, rtnType);
                if(null==rtnBean){
                    break;
                }
                JSONObject rtn = new JSONObject();
                rtn.put("rtnBeanId",rtnBean.getId());
                rtn.put("rtnName",rtnBean.getName());
                rtn.put("contain",rtnJson.getString("contain"));
                rtn.put("type",rtnJson.getString("type"));
                Map rtnMap=new HashMap();
                rtnMap.put("param",rtn.toJSONString());
                rtnConfig.setKVMap(rtnMap);
                configService.saveConfig(rtnConfig);
                method.setRtnConfigId(rtnConfig.getId());
            }
            uri= !StringUtils.isEmpty(uri) ? uri.substring(0,uri.length()-1) : "";
            Map selectMap = new HashMap<>();
            selectMap.put("name",methodJson.getString("name"));
            selectMap.put("paramUri",uri);
            selectMap.put("serviceId",serviceId);
            selectMap.put("type","sg");
            Method oldMethod = methodMapper.findByNameAndUriAndService(selectMap);
            if(null != oldMethod){
                return;
            }


            method.setName(methodJson.getString("name"));
            method.setmDesc(methodJson.getString("remarks"));
            method.setType("sg");
            method.setServiceId(serviceId);
            method.setParamUri(uri);
            Config config = new Config();
            Map map=new HashMap();
            map.put("param",mapList.toJSONString());
            config.setKVMap(map);
            configService.saveConfig(config);
            method.setParamConfigId(config.getId());
            this.saveMethod(method);
        }
    }

    /**
     * 保存方法
     * @param method
     * @return
     */
    @Override
    public Method saveMethod(Method method){
        method.setId(SeqGenerate.createId("METH"));
        methodMapper.insertSelective(method);
        return method;
    }

    @Override
    public List<Method> getMethods(String id,String serviceId, String type,String isParam) {
        if(StringUtils.isEmpty(isParam)){
            isParam="0";
        }
        Map selectMap=new HashMap<>();
        selectMap.put("serviceId",serviceId);
        selectMap.put("type",type);
        selectMap.put("id",id);
        List<Method> methods = methodMapper.getMethods(selectMap);
        if("1".equals(isParam)){
            for(Method method:methods){
                if("sg".equals(type)){
                    List<JSONObject> params = beanService.getParam(method.getParamConfigId());
                    if(params.size()>0){
                        method.setParams(params);
                    }
                    List<JSONObject> rtns = beanService.getRtn(method.getRtnConfigId());
                    if(rtns.size()>0){
                        method.setRtns(rtns);
                    }
                }else{
                    try {
                        Config paramConfig = configService.getConfigMap(method.getParamConfigId(), false);
                        List list=new ArrayList();
                        list.add(JSONObject.parseObject(paramConfig.getKVMap().get("param")));
                        method.setParams(list);


                        Config rtnConfig = configService.getConfigMap(method.getRtnConfigId(), false);
                        List rtnList=new ArrayList();
                        rtnList.add(JSONObject.parseObject(rtnConfig.getKVMap().get("rtn")));
                        method.setRtns(rtnList);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }
       return methods;
    }
}
